package cn.ekuma.epos.analysis.front;

import java.awt.Color;
import java.awt.Font;

import org.jdesktop.swingx.JXTable;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickMarkPosition;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import cn.ekuma.util.AbstractTimeGroupCalc;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JScrollPane;

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JSplitPane;

public class JPassengerFlowHistoryView extends javax.swing.JPanel{
	JFreeChart priceValueChart;
	AnalysisModelManager manager;
	ChartPanel chartPanel;
	private JXTable table;
	public JPassengerFlowHistoryView(AnalysisModelManager manager) {
		this.manager=manager;
		initComponents();
		table.setModel(manager.getPassengerFlowTransactionTableModel());
	}
	private void initComponents() {
		priceValueChart=createChart();
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		add(panel, BorderLayout.NORTH);
		
		JButton btnHour = new JButton("小时");
		btnHour.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				manager.createValueDataset(AbstractTimeGroupCalc.TIMERCYCLETYPE_Hour);
			}
		});
		panel.add(btnHour);
		
		JButton btnDay = new JButton("天");
		btnDay.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				manager.createValueDataset(AbstractTimeGroupCalc.TIMERCYCLETYPE_Today);
			}
		});
		panel.add(btnDay);
		
		JButton btnWeek = new JButton("星期");
		btnWeek.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				manager.createValueDataset(AbstractTimeGroupCalc.TIMERCYCLETYPE_Week);
			}
		});
		panel.add(btnWeek);
		
		JButton btnMonth = new JButton("月");
		btnMonth.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				manager.createValueDataset(AbstractTimeGroupCalc.TIMERCYCLETYPE_Month);
				
			}
		});
		panel.add(btnMonth);
		
		JSplitPane splitPane = new JSplitPane();
		splitPane.setOneTouchExpandable(true);
		splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
		add(splitPane, BorderLayout.CENTER);
		chartPanel=new ChartPanel(priceValueChart);
		splitPane.setLeftComponent(chartPanel);
		
		 JScrollPane scrollPane = new JScrollPane();
	     table = new org.jdesktop.swingx.JXTable();
	     table.setColumnControlVisible(true);
	     table.setAutoCreateRowSorter(true);
	     table.setAutoResizeMode(JXTable.AUTO_RESIZE_OFF);
	     scrollPane.setViewportView(table);
	     splitPane.setRightComponent(scrollPane);
	     splitPane.setDividerLocation(200);
	}
	
	
    private  JFreeChart createChart() {
    	JFreeChart chart = ChartFactory.createXYBarChart(
                "",
                "",
                true,
                "",
                manager.getValueTimeSeries(),
                PlotOrientation.VERTICAL,
                true,
                false,
                false
            );

            // then customise it a little...
           
            chart.setBackgroundPaint(Color.white);
            
            XYPlot plot = chart.getXYPlot();
            XYItemRenderer renderer = plot.getRenderer();
            //StandardXYToolTipGenerator generator = new StandardXYToolTipGenerator(
            //    "{1} = {2}", new SimpleDateFormat("yyyy"), new DecimalFormat("0")
            //);
            //renderer.setToolTipGenerator(generator);
            
            plot.setBackgroundPaint(Color.lightGray);
            plot.setRangeGridlinePaint(Color.white);
            DateAxis axis = (DateAxis) plot.getDomainAxis();
            axis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
            axis.setLowerMargin(1);
            axis.setUpperMargin(1);
            axis.setTickLabelFont(new Font("sans-serif", Font.PLAIN,15)); 
            return chart;
    }
}
